package com.kaiwukj.android.ufamily.mvp.ui.page.home.home;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ActiveResult;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ActiveHotAdapter extends BaseQuickAdapter<ActiveResult, BaseViewHolder> {
    public ActiveHotAdapter(@Nullable List<ActiveResult> data) {
        super(R.layout.item_active_card, data);
        addChildClickViewIds(R.id.btn_submit,R.id.btn_join);
    }

    @Override
    public long getItemId(int position) {
        ActiveResult item = getItem(position);
        if (item == null) return -1;
        return item.getId();
    }

    public int getPositionById(long id) {
        for (int i = 0; i < getData().size(); i++) {
            if (id == getData().get(i).getId()) {
                return i;
            }
        }
        return -1;
    }

    public void postJoin(int position, boolean join) {
        int oldNum = getItem(position).getJoinNum();
        if (join) {
            getItem(position).setJoinNum(oldNum + 1);
        } else {
            if (oldNum > 0) {
                getItem(position).setJoinNum(oldNum - 1);
            }
        }

        notifyItemChanged(position, "join");

    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ActiveResult item, @NotNull List<?> payloads) {
        super.convert(holder, item, payloads);
        if (payloads.isEmpty()) {
            convert(holder, item);
        } else {
            String tag = (String) payloads.get(0);
            if ("join".equals(tag)) {
                holder.setText(R.id.tv_join_num, String.format("%s人参与", item.getJoinNum()));
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, ActiveResult item) {

        //   ImageView ivWayFlag = helper.getView(R.id.iv_way_flag);

      /*  if (item.getPublisherType() == 1) {
            ivWayFlag.setImageResource(R.mipmap.ic_active_way_flag_01);
        } else if (item.getPublisherType() == 2) {
            ivWayFlag.setImageResource(R.mipmap.ic_active_way_flag_03);
        } else {
            ivWayFlag.setImageResource(R.mipmap.ic_active_way_flag_02);
        }*/

        helper.setText(R.id.tv_title, item.getActivityTitle());
        //helper.setText(R.id.tv_no, String.format("NO.%s", helper.getLayoutPosition() + 1));
        // helper.setText(R.id.tv_no, "HOT");
        ImageView ivImage = helper.getView(R.id.view_content);
        //helper.setText(R.id.tv_price, String.valueOf(item.getEnrollFee()));
        helper.setText(R.id.tv_time, String.format("活动时间: %s", item.getActivityStartTime()));
        helper.setText(R.id.tv_address, String.format("地址: %s", item.getActivityAddress()));
        SpannableString ss = new SpannableString(String.format("%s人参与", item.getJoinNum()));
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#333333"));
        ss.setSpan(colorSpan, ss.length() - 3, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_join_num, ss);
        Glide.with(getContext())
                .load(item.getActivityImg().split(",")[0])
                .placeholder(R.drawable.picture_image_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(ivImage);
    }
}
